import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/Login.vue'
import Home from '../views/Home/Home.vue'
import Welcome from '../views/Home/Welcome.vue'
import Users from '../views/Admin/Users'
import Rights from '../views/Rights/Rights'
import Role from '../views/Role/Role'
import Category from '../views/Goods/Category'
import Params from '../views/Goods/Params'
import SpecGroup from '../views/Goods/SpecGroup'
import Spec from '../views/Goods/Spec'
Vue.use(VueRouter)
const routes = [
    {
        path: '/',
        redirect: '/login'
    },

    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/users', component: Users },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Role },
            { path: '/category', component: Category },
            { path: '/params', component: Params },
            { path: '/specGroup', component: SpecGroup },
            { path: '/spec', component: Spec }
        ]
    },
    {
        path: '/about',
        name: 'About',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //from 代表从哪里来的
    //next 放行
    if (to.path == '/login') return next();
    //获取token
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    next();
})

export default router
